package com.xc.controller.backend;

import com.github.pagehelper.PageInfo;
import com.google.common.collect.Maps;
import com.xc.common.ServerResponse;
import com.xc.pojo.AgentUser;
import com.xc.pojo.SiteAdmin;
import com.xc.service.IFileUploadService;
import com.xc.service.ISiteAdminService;
import com.xc.utils.PropertiesUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Map;

/**
 * @author mipha
 * @version 1.0
 * @date 2019/2/26 11:04 AM
 */
@Controller
@RequestMapping("/admin/")
@Slf4j
public class AdminController {

    @Autowired
    ISiteAdminService iSiteAdminService;
    @Autowired
    IFileUploadService iFileUploadService;

    /**
     *  管理员列表
     * @param adminName
     * @param adminPhone
     * @param pageNum
     * @param pageSize
     * @param request
     * @return
     */
    @RequestMapping("list.do")
    @ResponseBody
    public ServerResponse<PageInfo> list(
            @RequestParam(value = "adminName",required = false) String adminName
            ,@RequestParam(value = "adminPhone",required = false) String adminPhone
            ,@RequestParam(value = "pageNum", defaultValue = "1") int pageNum
            ,@RequestParam(value = "pageSize", defaultValue = "10") int pageSize
            ,HttpServletRequest request){
        return iSiteAdminService.listByAdmin(adminName,adminPhone,request,pageNum,pageSize);
    }


    /**
     *  修改管理员锁定状态
     * @param adminId
     * @return
     */
    @RequestMapping("updateLock.do")
    @ResponseBody
    public ServerResponse updateLock(Integer adminId){
        return iSiteAdminService.updateLock(adminId);
    }


    /**
     *  添加管理员
     * @param siteAdmin
     * @return
     */
    @RequestMapping("add.do")
    @ResponseBody
    public ServerResponse add(SiteAdmin siteAdmin){
        return iSiteAdminService.add(siteAdmin);
    }


    /**
     *  修改管理员
     * @param siteAdmin
     * @return
     */
    @RequestMapping("update.do")
    @ResponseBody
    public ServerResponse update(SiteAdmin siteAdmin){
        return iSiteAdminService.update(siteAdmin);
    }



    /**
     *  管理后台 统计信息
     * @return
     */
    @RequestMapping("count.do")
    @ResponseBody
    public ServerResponse count(){
        return iSiteAdminService.count();
    }




    /**
     *  管理员上传图片
     * @param session
     * @param file
     * @param request
     * @return
     */
    @RequestMapping("upload.do")
    @ResponseBody
    public ServerResponse upload(HttpSession session
            , @RequestParam(value = "upload_file" , required = false) MultipartFile file
            , HttpServletRequest request){

        String path = request.getSession().getServletContext().getRealPath("upload");

        ServerResponse serverResponse = iFileUploadService.upload(file,path);
        if(serverResponse.isSuccess()){
            String targetFileName = serverResponse.getData().toString();
            String url = PropertiesUtil.getProperty("ftp.server.http.prefix")
                    + targetFileName;

            Map fileMap = Maps.newHashMap();
            fileMap.put("uri" , targetFileName);
            fileMap.put("url" , url);

            return ServerResponse.createBySuccess(fileMap);
        }else{
            return serverResponse;
        }
    }



}
